import os
import cv2
import numpy as numpy
from flask import Flask, render_template, Response


root_path = './Line_Dataset'
if not os.path.exits(root_path):
    os.system('sh ~/get_data.sh')
all_videos = os.listdir(root_path)

app = Flask(__name__)


@app.route("/")
def index():
    return render_template("video.html")
    # return "hello"

def generate_frame():
    for video in all_videos:
        video_path = os.path.join(root_path, video)
        cap = cv2.VideoCapture(video_path)
        if not cap.isOpened():
            print('can not open camera')
            break
        while True:
            ret, frame = cap.read()
            if not ret:
                break
            ret1, buffer = cv2.imencode('.jpg', frame)
            frame = buffer.tobytes()
            yield  (b'--frame\r\n'
                    b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')


@app.route('/video_feed0')
def video_feed0():
    return Response(generate_frame(),mimetype='multipart/x-mixed-replace; boundary=frame')


if __name__=='__main__':
    app.run(host='0.0.0.0', port=5555)